<?php

namespace app\service\controller;

use app\admin\model\Service;
use think\Controller;

class Login extends Controller
{
    public function index()
    {
        if (isMobile()) {
            $this->redirect('service/index/mobile');
        }
        $this->assign([
            'version' => config('version')
        ]);

        return $this->fetch();
    }

    public function doLogin()
    {
        if (request()->isAjax()) {

            $userName = input('post.username');
            $password = input('post.password');

            $user = db('services')->where('user_name', $userName)->find();
            if (empty($user)) {
                return json(['code' => -1, 'data' => '', 'msg' => '客服不存在']);
            }

            if ($user['status'] == 2) {
                return json(['code' => -1, 'data' => '', 'msg' => '客服被禁用']);
            }

            if (md5($password . config('salt')) != $user['user_pwd']) {
                return json(['code' => -2, 'data' => '', 'msg' => '密码错误']);
            }

            // 设置session标识状态
            if ($user['role'] == Service::ROLE_ADMIN) {
                cookie('l_user_admin', 1, config('save_time'));
            } else {
                cookie('l_user_admin', 0, config('save_time'));
            }
            cookie('l_user_name', $user['user_name'], config('save_time'));
            cookie('l_user_id', $user['id'], config('save_time'));
            cookie('l_user_avatar', $user['user_avatar'], config('save_time'));

            return json(['code' => 1, 'data' => url('index/index'), 'msg' => '登录成功']);
        }

        $this->error('非法访问');
    }

    public function loginOut()
    {
        cookie('l_user_name', null);
        cookie('l_user_id', null);
        cookie('l_user_avatar', null);

        $this->redirect(url('login/index'));
    }
}
